import scrapy
from scrapy import Request
from scrapy.selector import Selector
from Games.items import GameListItem
import json

class SchoolSpider(scrapy.Spider):
    name = 'game'
    allowed_domains=['taptap.com']
    start_urls=['https://www.taptap.com/ajax/top/download?page=1&total=30']

    s_c = 0
    page=1
    def parse(self, response):
        rs = json.loads(response.text)
        if True:
            data = rs.get('data')
            html = data.get('html')
            game_list =Selector(text=html).xpath('//div[@class="taptap-top-card"]')
            for game in game_list[1::1]:
                game_list_item=GameListItem()
                game_list_item['game_name']=game.xpath('div[@class="top-card-middle"]/a/h4/text()').extract_first().replace(" ","")
                game_list_item['game_author'] = game.xpath( 'div[@class="top-card-middle"]/p[@class="card-middle-author"]/a/text()').extract_first().replace(" ","")
                game_list_item['game_description']=game.xpath('div[@class="top-card-middle"]/p[@class="card-middle-description"]/text()').extract_first().replace("'","‘")
                game_list_item['game_score'] =game.xpath('div[@class="top-card-middle"]/div[@class="card-middle-score"]/p/span/text()').extract_first().replace(" ","")

                #game_list_item['game_tags'] = game.xpath('div[@class="top-card-middle"]/div[@class="card-tags"]/a/text()').extract_first()

                game_list_item['game_type'] = game.xpath('div[@class="top-card-middle"]/div[@class="card-middle-category"]/a/text()').extract_first().replace(" ","")
                self.s_c=self.s_c+1
                game_list_item['game_rank'] = self.s_c

                yield game_list_item
        self.page=self.page+1
        # next_page=response.xpath('//section[@class="taptap-button-more load-more"]/button/@data-url').extract_first()
        # if next_page is not None:
        if(self.page<6):
            url="https://www.taptap.com/ajax/top/download?page="+str(self.page) +"&total=30"
            yield Request(url,callback=self.parse)





















